# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import json
from datetime import datetime
from .items import ImoocFreeCourseListItem
import os


def get_file_pre():
    now_datetime = datetime.now()
    return str(now_datetime.year) + '-' + str(now_datetime.month) + '-' + str(now_datetime.day)


class ImoocPipeline(object):
    def process_item(self, item, spider):
        return item


class ImoocFreeCourseJsonPipline(object):

    def open_spider(self, spider):
        # 如果数据存储目录不存在, 手动创建目录; 如: data/2019-10-10
        self.file_path = './data/' + get_file_pre()
        if not os.path.exists(self.file_path):
            os.makedirs(self.file_path)

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        if isinstance(item, ImoocFreeCourseListItem):
            self.file_name = 'free_course_list' + '.jl'
        print(self.file_path, '/', self.file_name)
        self.file = open(self.file_path + '/' + self.file_name, 'a')
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        return item
